#!/usr/bin/ruby
#-*- coding: UTF-8 -*-
require 'xcodeproj'
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

# _xcodeProjPath = "/Users/admin/Documents/workspace/b2/release/0.4.1/frameworks/runtime-src/proj.ios_appstore_yuntiankong_iosappstore_nzhc/arpg.xcodeproj"#ARGV[0]
# _originPlatform = "yuntiankong_iosappstore_nzhc"#ARGV[1]
# _workPlatform = "yuntiankong_iosappstore_nzhc"#ARGV[2]
# _bundleID = "com.new.bundle.id"#ARGV[3]
# _gameVersion = "1992.10.06"#ARGV[4]
# _gameChineseName = "我的第一个游戏"#ARGV[5]
_xcodeProjPath = ARGV[0]
_originPlatform = ARGV[1]
_workPlatform = ARGV[2]
_bundleID = ARGV[3]
_gameVersion = ARGV[4]
_gameChineseName = ARGV[5]
_packChoice = ARGV[6]
_targetName = ARGV[7]
#修改为UTF-8编码
_gameChineseName = (_gameChineseName.is_a? String) ? _gameChineseName.dup.force_encoding('UTF-8') : _gameChineseName.dup

project_path = _xcodeProjPath
puts project_path
platform_project = Xcodeproj::Project.open(project_path)
platform_target = platform_project.targets[0]
platform_target.name = _targetName
#puts platform_target.name
#puts platform_target.platform_name
#puts platform_target.display_name


#获得文件目录并修改plish文件
main_group_path = platform_project.main_group.real_path
main_group_children_array = platform_project.main_group.children
main_group_children_array.each do |main_children_file|
	# puts main_children_file.path
	# puts main_children_file.name
	# puts main_children_file.class
	# puts "\n"
	if "#{main_children_file.path}" == "ios"
		#puts "ios"
		#puts main_children_file.class
	 	ios_file = main_children_file
	 	ios_file.children.each do |ios_file_reference|
	 		#puts "ios_children"
	 		#puts ios_file_reference.class
	 		if "#{ios_file_reference.name}" == "Info.plist"
	 			if !_workPlatform.empty? or !_bundleID.empty?
	 				puts "start to config info.plist in ios folder"
	 				info_file = ios_file_reference
	 				info_file.children.each do |info_file_sub|
	#					puts info_file_sub.class
	# 					puts info_file_sub.real_path
	 					file_content_hash = Xcodeproj::Plist.read_from_path(info_file_sub.real_path)
	 					#puts file_content_hash
	 					if !_workPlatform.empty?
	 						file_content_hash["CFBundleDisplayName"] = _workPlatform
	 					end
	 					if !_bundleID.empty?
	 						file_content_hash["CFBundleIdentifier"] = _bundleID
	 					end
	 					Xcodeproj::Plist.write_to_path(file_content_hash,info_file_sub.real_path)
	 					#puts file_content_hash
	 				end
	 				puts "end config info.plist in ios folder"
                	puts "\n"
	 			end
	 		end
            if "#{ios_file_reference.name}" == "InfoPlist.strings" and !_gameChineseName.empty?
                puts "start to config InfoPlist.strings in ios folder"
                infoStr_file = ios_file_reference
                infoStr_file.children.each do |infoStr_file_sub|
#                   puts infoStr_file_sub.class
                    file_content_hash = Xcodeproj::Plist.read_from_path(infoStr_file_sub.real_path)
#                   puts file_content_hash
                    file_content_hash["CFBundleDisplayName"] = _gameChineseName
                    file_content_str = file_content_hash.to_s.gsub("=>","=")
					file_content_str = file_content_str.gsub("{","{\n\t")
					file_content_str = file_content_str.gsub(", ",";\n\t")
					file_content_str = file_content_str.gsub("}",";\n}")
					aFile = File.new(infoStr_file_sub.real_path, "w+")
					if aFile
   						aFile.syswrite(file_content_str)
					end
					aFile.close
                    # Xcodeproj::Plist.write_to_path(file_content_hash,infoStr_file_sub.real_path)
                end
                puts "end config InfoPlist.strings in ios folder"
                puts "\n"
            end
	 	end
	end
end

#获得build setting
platform_target.build_configurations.each do |build_config|
#	puts config.name
#	puts config.build_settings
# if config.name == 'Debug'
	config_hash = build_config.build_settings
	config_hash.each do |key, value|
		#print key, " is ", value, "\n"
		if key == "INFOPLIST_FILE" and !_gameChineseName.empty?
			puts "start to config info.plist in build_setting"
			config_info_plist_path = "#{main_group_path}/#{value}"
#            puts config_info_plist_path
			info_plist_hash = Xcodeproj::Plist.read_from_path(config_info_plist_path)
#            puts info_plist_hash
			info_plist_hash["CFBundleDisplayName"] = _gameChineseName
			puts "start to write#{_gameChineseName}"
			Xcodeproj::Plist.write_to_path(info_plist_hash,config_info_plist_path)
			puts "end config info.plist in build_setting"
            puts "\n"
		end
		if key == "PRODUCT_BUNDLE_IDENTIFIER" and !_bundleID.empty?
			puts "start to config build setting(product_bundle_id)"
			config_hash["PRODUCT_BUNDLE_IDENTIFIER"] = _bundleID
			puts "end config build setting(product_bundle_id)"
            puts "\n"
		end
		if key == "PRODUCT_NAME" and !_targetName.empty?
			puts "start to config build setting(product_name)"
			config_hash["PRODUCT_NAME"] = _targetName
			puts "end config build setting(product_name)"
            puts "\n"
		end
	end
# end
end

# #获得build phases
platform_target.build_phases.each do |sub_file|
	#    puts sub_file.class
	if "#{sub_file.class}" == "Xcodeproj::Project::Object::PBXShellScriptBuildPhase"
		run_script_file = sub_file
		#puts run_script_file.shell_script
		puts "start to config run script"
		script = run_script_file.shell_script
		if !_originPlatform.empty? and !_workPlatform.empty?
			script = script.gsub("#{_originPlatform}","#{_workPlatform}")
		end
		if !_targetName.empty?
			script = script.gsub(/TARGET_BUILD_DIR\"\/.*\.app\b/,"TARGET_BUILD_DIR\"/#{_targetName}.app")
		end
		script_command_array = script.split("\n")
#       puts script_array.size
#       puts "#{array}"
        last_script_str = script_command_array.last
        modify_script_array = last_script_str.split(" ")
        # puts modify_script_array.size
        # puts "#{modify_script_array}"
        # puts "#{modify_script_array[4]}"
		if modify_script_array.size >= 9 and (!_gameVersion.empty? or _packChoice != "none")
			if !_gameVersion.empty?
				#puts "change game version"
				isPythonScript = 0
        		if modify_script_array[0] == "python"
        			isPythonScript = 1
        		end
				modify_script_array[4 + isPythonScript] = "\"#{_gameVersion}\""
			end
			if modify_script_array.last == "6" and modify_script_array[modify_script_array.size - 2] == "-z" and _packChoice == "small"
				modify_script_array.pop
				modify_script_array.pop
				#puts "set as small package"
			end
			if modify_script_array.last != "6" and _packChoice == "big"
				modify_script_array.push("-z")
				modify_script_array.push("6")
				#puts "set as big package"
			end
		end
        # puts "#{modify_script_array}"
		
        last_script_str = modify_script_array.join(" ")
        script_command_array.pop
        script_command_array.push(last_script_str)
        script = script_command_array.join("\n")
        # puts script
#        puts script
		run_script_file.shell_script = script
		puts "end config run script"
        puts "\n"
	end
end

puts "start to recreate schemes"
platform_project.recreate_user_schemes(visible = true)
puts "end recreate schemes"

puts "save modify"
platform_project.save

